{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ce9e10ef",
   "metadata": {},
   "source": [
    "# Python 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a01c96ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "mylist = [\"apple\", \"banana\", \"cherry\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "633d4a08",
   "metadata": {},
   "source": [
    "## 列表\n",
    "列表用于在单个变量中存储多个项目。\n",
    "\n",
    "列表是 Python 中用于存储数据集合的 4 种内置数据类型之一，其他 3 种是元组Tuple、集合Set和字典Dictionary，它们具有不同的性质和用法。\n",
    "\n",
    "列表是使用方括号创建的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "843118b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef3f1afc",
   "metadata": {},
   "source": [
    "## 构造函数 list()\n",
    "创建新列表时也可以使用 list() 构造函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a0aaae22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = list((\"apple\", \"banana\", \"cherry\")) # note the double round-brackets\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fda2144b",
   "metadata": {},
   "source": [
    "## 列表项\n",
    "列表项是有序的、可变的，并允许重复值。\n",
    "\n",
    "列表项已编入索引，第一个项的索引为 [0]，第二个项的索引为 [1]，以此类推。\n",
    "### 有序的\n",
    "当我们说列表是有序的时，这意味着项目具有定义的顺序，并且该顺序不会改变。\n",
    "\n",
    "如果向列表中添加新项目，则新项目将放置在列表的末尾。\n",
    "### 可变的\n",
    "列表是可变的，这意味着我们可以在创建列表后更改、添加和删除列表中的项目。\n",
    "\n",
    "### 允许重复\n",
    "由于列表已编入索引，因此列表可以包含具有相同值的项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a897325c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'apple', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\"]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b03d0544",
   "metadata": {},
   "source": [
    "## 列表项 - 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b9711d76",
   "metadata": {},
   "outputs": [],
   "source": [
    "list1 = [\"apple\", \"banana\", \"cherry\"]\n",
    "list2 = [1, 5, 7, 9, 3]\n",
    "list3 = [True, False, False]\n",
    "\n",
    "list4 = [\"abc\", 34, True, 40, \"male\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8648d8de",
   "metadata": {},
   "source": [
    "## 列表长度\n",
    "要确定列表有多少项，请使用 len() 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "01612e4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(len(thislist))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37b65866",
   "metadata": {},
   "source": [
    "# 访问项目\n",
    "列表项已编入索引，可以通过引用索引号来访问它们："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e198ac47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "banana\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(thislist[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aa460b0",
   "metadata": {},
   "source": [
    "## 负索引意味着从尾开始\n",
    "\n",
    "-1 指最后一项，-2 指倒数第二项，以此类推。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5fc81e56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(thislist[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21f72a52",
   "metadata": {},
   "source": [
    "## 索引范围\n",
    "您可以通过指定范围的开始和结束位置来指定索引范围。\n",
    "\n",
    "指定范围时，返回值将是具有指定项目的新列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73cb3041",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cherry', 'orange', 'kiwi']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\"]\n",
    "print(thislist[2:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ee06d0f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'orange']\n"
     ]
    }
   ],
   "source": [
    "# 此示例返回从开头到但不包括“kiwi”的项目：\n",
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\"]\n",
    "print(thislist[:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "33d07f04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cherry', 'orange', 'kiwi', 'melon', 'mango']\n"
     ]
    }
   ],
   "source": [
    "# 此示例返回从“cherry”到末尾的项目：\n",
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\"]\n",
    "print(thislist[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2202f5cc",
   "metadata": {},
   "source": [
    "## 负索引范围\n",
    "如果要从列表末尾开始搜索，请指定负索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d4bb3e9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['orange', 'kiwi', 'melon']\n"
     ]
    }
   ],
   "source": [
    "# 此示例返回从“orange”(-4) 到但不包括“mango”(-1) 的项目：\n",
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\"]\n",
    "print(thislist[-4:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07b0b2d",
   "metadata": {},
   "source": [
    "## 检查项目是否存在\n",
    "要确定列表中是否存在指定的项目，请使用 in 关键字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "96c4a140",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, 'apple' is in the fruits list\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "if \"apple\" in thislist:\n",
    "  print(\"Yes, 'apple' is in the fruits list\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab642cba",
   "metadata": {},
   "source": [
    "# 更改项目值\n",
    "要更改特定项目的值，请参阅索引号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0778197a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'blackcurrant', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist[1] = \"blackcurrant\"\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4432c79",
   "metadata": {},
   "source": [
    "## 更改特定范围内项目的值\n",
    "要更改特定范围内项目的值，请定义一个包含新值的列表，并参考要插入新值的索引号范围："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a31541e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"mango\"]\n",
    "thislist[1:3] = [\"blackcurrant\", \"watermelon\"]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97956ee9",
   "metadata": {},
   "source": [
    "如果插入的项目多于替换的项目，新项目将插入您指定的位置，其余项目将相应移动："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "16991ee3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'blackcurrant', 'watermelon', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist[1:2] = [\"blackcurrant\", \"watermelon\"]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87b70e52",
   "metadata": {},
   "source": [
    "如果您插入的项目少于替换的项目，新项目将插入您指定的位置，其余项目将相应移动："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5fc883ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'watermelon']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist[1:3] = [\"watermelon\"]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c201c22",
   "metadata": {},
   "source": [
    "## 插入项目\n",
    "要插入新的列表项，而不替换任何现有值，我们可以使用 insert() 方法。\n",
    "\n",
    "insert() 方法在指定的索引处插入一个项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8543714a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'watermelon', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.insert(2, \"watermelon\")\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2915aeed",
   "metadata": {},
   "source": [
    "## 追加项目\n",
    "要将项目添加到列表的末尾，请使用 append() 方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "aec72a7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'orange']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.append(\"orange\")\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6e93fcb",
   "metadata": {},
   "source": [
    "## 扩展列表\n",
    "要将另一个列表中的元素附加到当前列表，请使用 extend() 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "23722cc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "tropical = [\"mango\", \"pineapple\", \"papaya\"]\n",
    "thislist.extend(tropical)\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42271e39",
   "metadata": {},
   "source": [
    "extend() 方法不必附加列表，您可以添加任何可迭代对象（元组、集合、字典等）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "91498530",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry', 'kiwi', 'orange']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thistuple = (\"kiwi\", \"orange\")\n",
    "thislist.extend(thistuple)\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37a18167",
   "metadata": {},
   "source": [
    "## 删除指定项目\n",
    "remove() 方法删除指定的项目。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "57760716",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.remove(\"banana\")\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dcb8f1b",
   "metadata": {},
   "source": [
    "## 删除指定索引\n",
    "pop() 方法删除指定的索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "301692ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.pop(1)\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c9c934ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana']\n"
     ]
    }
   ],
   "source": [
    "# 如果不指定索引，pop() 方法将删除最后一项。\n",
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.pop()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7613ac5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "# del 关键字也会删除指定的索引：\n",
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "del thislist[0]\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2e6e1ce9",
   "metadata": {},
   "outputs": [],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "del thislist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24444da2",
   "metadata": {},
   "source": [
    "## 清空列表\n",
    "clear() 方法清空列表。\n",
    "\n",
    "该列表仍然存在，但没有内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bbff41c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "thislist.clear()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e31a0c69",
   "metadata": {},
   "source": [
    "# 循环遍历列表\n",
    "可以使用 for 循环遍历列表项："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5952b533",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "for x in thislist:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d23ab28",
   "metadata": {},
   "source": [
    "## 遍历索引号\n",
    "您还可以通过引用它们的索引号来循环遍历列表项。\n",
    "\n",
    "使用 range() 和 len() 函数创建合适的可迭代对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0956b752",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 3)\n",
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "count = range(len(thislist))\n",
    "print(count)\n",
    "for i in count:\n",
    "  print(thislist[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a2b809e",
   "metadata": {},
   "source": [
    "## 使用 While 循环\n",
    "您可以使用 while 循环遍历列表项。\n",
    "\n",
    "使用 len() 函数来确定列表的长度，然后从 0 开始并通过引用它们的索引来循环遍历列表项。\n",
    "\n",
    "请记住在每次迭代后将索引增加 1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c09e32f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "i = 0\n",
    "while i < len(thislist):\n",
    "  print(thislist[i])\n",
    "  i = i + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e571f01",
   "metadata": {},
   "source": [
    "# List Comprehension\n",
    "当您想要基于现有列表的值创建新列表时，List Comprehension提供了更短的语法。\n",
    "\n",
    "例子：\n",
    "\n",
    "根据水果列表，您需要一个新列表，其中仅包含名称中带有字母“a”的水果。\n",
    "\n",
    "如果没有List Comprehension，您将不得不编写一个带有条件测试的 for 语句："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f067e699",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'mango']\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\", \"kiwi\", \"mango\"]\n",
    "newlist = []\n",
    "\n",
    "for x in fruits:\n",
    "  if \"a\" in x:\n",
    "    newlist.append(x)\n",
    "\n",
    "print(newlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "0c55f32c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['APPLE', 'BANANA', 'MANGO']\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\", \"kiwi\", \"mango\"]\n",
    "newlist = [x.upper() for x in fruits if \"a\" in x]\n",
    "print(newlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c03ca39b",
   "metadata": {},
   "source": [
    "## 语法\n",
    "newlist = [expression for item in iterable if condition == True]\n",
    "\n",
    "返回值是一个新列表，旧列表保持不变。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00aef077",
   "metadata": {},
   "source": [
    "# 排序列表\n",
    "默认情况下，列表对象有一个 sort() 方法，该方法将按字母数字、升序对列表进行排序："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "35a27fe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['banana', 'kiwi', 'mango', 'orange', 'pineapple']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"orange\", \"mango\", \"kiwi\", \"pineapple\", \"banana\"]\n",
    "thislist.sort()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "bf3f60bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 50, 65, 82, 100]\n"
     ]
    }
   ],
   "source": [
    "thislist = [100, 50, 65, 82, 23]\n",
    "thislist.sort()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8c0c23c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Kiwi', 'Orange', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "# 默认情况下， sort() 方法区分大小写，导致所有大写字母都排在小写字母之前：\n",
    "thislist = [\"banana\", \"Orange\", \"Kiwi\", \"cherry\"]\n",
    "thislist.sort()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9a4e500c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['pineapple', 'orange', 'mango', 'kiwi', 'banana']\n"
     ]
    }
   ],
   "source": [
    "# 降序排列\n",
    "thislist = [\"orange\", \"mango\", \"kiwi\", \"pineapple\", \"banana\"]\n",
    "thislist.sort(reverse = True)\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5c18a7c",
   "metadata": {},
   "source": [
    "## 自定义排序函数\n",
    "您还可以使用关键字参数 key = function 自定义您自己的函数。\n",
    "\n",
    "该函数将返回一个用于对列表进行排序的数字（最小的数字在前）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e60b67ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[50, 65, 23, 82, 100]\n"
     ]
    }
   ],
   "source": [
    "def myfunc(n):\n",
    "  return abs(n - 50)\n",
    "\n",
    "thislist = [100, 50, 65, 82, 23]\n",
    "thislist.sort(key = myfunc)\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad069448",
   "metadata": {},
   "source": [
    "## 相反的顺序\n",
    "如果您想颠倒列表的顺序，而不考虑字母表怎么办？\n",
    "\n",
    "reverse() 方法反转元素的当前排序顺序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "eebd44aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cherry', 'Kiwi', 'Orange', 'banana']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"banana\", \"Orange\", \"Kiwi\", \"cherry\"]\n",
    "thislist.reverse()\n",
    "print(thislist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e969a44f",
   "metadata": {},
   "source": [
    "# 复制列表\n",
    "不能简单地通过键入 list2 = list1 来复制列表，因为： list2 将只是对 list1 的引用，并且在 list1 中所做的更改也会自动在 list2 中进行。\n",
    "\n",
    "有多种方法可以进行复制，一种方法是使用内置的 List 方法 copy()。另一种方法是使用内置方法 list()。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "4fb8d9a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "mylist = thislist.copy()\n",
    "print(mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "aeb8acac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\"]\n",
    "mylist = list(thislist)\n",
    "print(mylist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b5742c5",
   "metadata": {},
   "source": [
    "# 连接两个列表\n",
    "在 Python 中有多种方法可以连接或连接两个或多个列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a6993547",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "# 最简单的方法之一是使用 + 运算符。\n",
    "list1 = [\"a\", \"b\", \"c\"]\n",
    "list2 = [1, 2, 3]\n",
    "\n",
    "list3 = list1 + list2\n",
    "print(list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9c2b0486",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "list1 = [\"a\", \"b\" , \"c\"]\n",
    "list2 = [1, 2, 3]\n",
    "\n",
    "for x in list2:\n",
    "  list1.append(x)\n",
    "\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0e8c375b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "list1 = [\"a\", \"b\" , \"c\"]\n",
    "list2 = [1, 2, 3]\n",
    "\n",
    "list1.extend(list2)\n",
    "print(list1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
